home *** CD-ROM | disk | FTP | other *** search
- /*
- File: MemoryHe.h
-
- Contains: MemoryHeap class interface
-
- Owned by: Michael Burbidge, Jens Alfke
- Owned by: Jens Alfke
-
- Copyright: © 1993 - 1996 by Apple Computer, Inc., all rights reserved.
-
- Change History (most recent first):
-
- <3> 27.09.1996 NP 1386083: Changes to Allocate
- <2> 9/13/96 jpa 1371387: Speed optimizations.
- <11> 10/24/95 jpa 1293441: Added slush-fund.
- <10> 8/4/95 DM Leak checking [1267956]
- <9> 5/4/95 jpa Support for finding largest free block
- [1235657] and validating memory ranges
- [1246077]
- <8> 10/24/94 jpa Constness [1194286].
- <7> 9/29/94 RA 1189812: Mods for 68K build.
- <6> 9/14/94 jpa Eliminated dependencies on rest of OpenDoc.
- Added ability to get heap of a block.
- [1186692]
- <5> 8/17/94 jpa Added support for walking heaps [1179567].
- <4> 8/8/94 jpa Added oldBlk param to DidRealloc hook
- [1179567]
- <3> 6/28/94 jpa Added no-op new/delete to MemoryHookList to
- avoid refs to ODDisposePtr.
- <2> 6/13/94 MB Some more initial fixes
- <2> 6/10/94 MB Make it build
- <1> 6/9/94 MB first checked in
- <3> 5/26/94 MB #1162181: Fixed MMM integration bug
- <2> 5/9/94 MB #1162181: Changes necessary to install MMM.
- <1> 4/29/94 MB first checked in
- To Do:
- In Progress:
-
- */
-
- #ifndef _MEMORYHE_
- #define _MEMORYHE_
-
- #ifndef _PLATFMEM_
- #include "PlatfMem.h"
- #endif
-
- #ifndef _MEMHOOK_
- #include "MemHook.h"
- #endif
-
- #ifndef __STDDEF__
- #include <stddef.h>
- #endif
-
- #ifndef __TYPES__
- #include <Types.h>
- #endif
-
-
- //----------------------------------------------------------------------------------------
- // HeapWalkProc (proc-ptr type)
- //----------------------------------------------------------------------------------------
-
- typedef MMBoolean (*HeapWalkProc)( const void *blk, unsigned long size, MMBoolean isObject,
- void *refCon );
-
- class StackCrawl; // For GetBlockStackCrawl
-
-
- //========================================================================================
- // MemoryHeap
- //
- // Abstract base class for memory heaps.
- //
- //========================================================================================
-
- class MemoryHeap
- {
- public:
- enum { kBlockTypeId = 0 };
- enum { kMagicNumber = 0x8765FEDC };
-
- // Keep a list of all heaps created. Very useful for debugging.
-
- static MemoryHeap* fHeapList;
- static MemoryHeap* GetFirstHeap();
-
- void* Allocate(ODBlockSize size);
- ODBlockSize BlockSize(const void* block) const;
- virtual unsigned long BytesAllocated() const;
- virtual unsigned long BytesFree() const = 0;
- virtual unsigned long LargestFreeBlock() const;
-
- void Free(void*);
- virtual MemoryHeap* GetNextHeap() const;
- virtual unsigned long HeapSize() const = 0;
- virtual unsigned long NumberAllocatedBlocks() const;
- // void Reset();
- void* Reallocate(void* block, ODBlockSize newSize);
-
- inline MMHeapLocation GetLocation( ) {return fMemSource;}
-
- void* operator new(SIZE_T size, MMHeapLocation =kMMTempMemory);
- void operator delete(void* ptr);
-
- virtual ~MemoryHeap();
-
- static const char* kDefaultDescription;
- static const char* kDeadHeapDescription;
- virtual const char* GetDescription() const;
- virtual void SetDescription(const char* description = kDefaultDescription);
-
- // Access to the isObject flag of a block. The intended use is that 'operator new'
- // will set the flag for all SOM objects, giving us a way to tell whether any block
- // is an object. This can help a lot in debugging. --jpa
- void SetBlockIsObject( void* blk, mmboolean isObject );
- mmboolean BlockIsObject( const void* blk ) const;
-
- MemoryHeap* GetBlockHeap( const void* ) const; // Should be static but can't be made so
-
- mmboolean AllocateSlushFund( size_t size, size_t allocSizeLimit );
- size_t GetSlushFundSize( ) {return fSlushFundSize;}
- size_t FreeSlushFund( ); // returns size freed
-
- #if MM_DEBUG
- virtual void AdoptHook(ODMemoryHook* ODMemoryHook);
- virtual void DeleteHook(ODMemoryHook* ODMemoryHook);
-
- virtual void SetZapOnAllocate(mmboolean = false);
- virtual void SetZapOnFree(mmboolean = false);
- virtual mmboolean GetZapOnAllocate() const;
- virtual mmboolean GetZapOnFree() const;
- virtual mmboolean GetAutoValidation() const;
- virtual void SetAutoValidation(mmboolean = false);
- mmboolean IsValidBlock(const void* blk) const;
- virtual mmboolean IsMyBlock(const void* blk) const = 0;
- mmboolean FindBlockContaining( const void *start, const void *end,
- const void* &blockStart, const void* &blockEnd ) const;
-
- ODBlockSize GetBlockRealSize( const void* blk ) const;
-
- virtual void Check( HeapWalkProc proc =NULL, void *refCon =NULL ) const = 0;
- virtual void Print(char* msg = "") const = 0;
-
- // Access to block StackCrawl.
- void SetBlockStackCrawl( const void* blk, StackCrawl* );
- StackCrawl* GetBlockStackCrawl( const void* blk ) const;
- #endif
-
- MemoryHeap(mmboolean autoValidation = false,
- mmboolean zapOnAllocate = true,
- mmboolean zapOnFree = false,
- MMHeapLocation =kMMTempMemory);
-
- virtual void* AllocateRawMemory(ODBlockSize size);
- virtual void* DoAllocate(ODBlockSize size, ODBlockSize& allocatedSize) = 0;
- virtual ODBlockSize DoBlockSize(const void* block) const = 0;
- virtual void DoFree(void*) = 0;
- // virtual void DoReset() = 0;
- virtual void* DoReallocate(void* block, ODBlockSize newSize, ODBlockSize& allocatedSize);
- virtual void FreeRawMemory(void* ptr);
- virtual unsigned long DoLargestFreeBlock() const = 0;
-
- virtual void DoSetBlockIsObject( void* ptr, mmboolean isObject ) = 0;
- virtual mmboolean DoBlockIsObject( const void* ptr ) const = 0;
- virtual MemoryHeap* DoGetBlockHeap( const void* ) const = 0;
-
- #if MM_DEBUG
- virtual mmboolean DoIsValidBlock(const void* blk) const = 0;
- virtual mmboolean DoFindBlockContaining( const void *start, const void *end,
- const void* &blockStart, const void* &blockEnd ) const = 0;
- virtual void CompilerCheck();
- void SetMemFullMark();
- void UnsetMemFullMark();
- #endif
-
- #if MM_DEBUG
- mmboolean ValidateMagicNumber( ) const;
- #else
- inline void ValidateMagicNumber( ) const{ }
- #endif
-
- protected:
- void AddToBytesAllocated( ODBlockSize bytes ) {fBytesAllocated += bytes;}
- void SubtractFromBytesAllocated( ODBlockSize bytes ) {fBytesAllocated -= bytes;}
-
- private:
- MemoryHeap* fNextHeap;
- MMHeapLocation fMemSource;
- #if MM_DEBUG
- mmboolean fZapOnAllocate;
- mmboolean fZapOnFree;
- mmboolean fAutoValidation;
- size_t fMemFullMark; // 0 means there is no mark.
- #endif
- unsigned long fBytesAllocated;
- unsigned long fNumberAllocatedBlocks;
- long fMagicNumber;
- MMBlock fSlushFund;
- size_t fSlushFundSize;
- size_t fSlushFundAllocSizeLimit; // Max request size to use slush fund for
-
- enum { kDescriptionLength = 64 };
- char fDescription[kDescriptionLength];
-
- #if MM_DEBUG
- MemoryHookList fMemoryHookList;
-
- protected:
- ODBlockSize CallGetHeaderSize() const;
-
- ODBlockSize CallAboutToAllocateHooks(ODBlockSize size) const;
- void* CallDidAllocateHooks(void* blk, ODBlockSize size);
- const void* CallAboutToBlockSizeHooks(const void* blk) const;
- void* CallAboutToFreeHooks(void* blk);
- void CallAboutToReallocHooks(void*& blk, ODBlockSize& size);
- void* CallDidReallocHooks(void* oldBlk, void* blk, ODBlockSize size);
- void CallAboutToResetHooks();
- void CallCommentHooks(const char* comment);
- void ValidateAndReport(void* blk) const;
- #endif
-
- private:
- MemoryHeap(const MemoryHeap& blk);
- MemoryHeap& operator=(const MemoryHeap& blk);
- // This class shouldn't be copied.
- };
-
-
- //----------------------------------------------------------------------------------------
- // MemoryHeap::GetBlockHeap
- //----------------------------------------------------------------------------------------
- inline MemoryHeap* MemoryHeap::GetBlockHeap( const void* ptr ) const
- {
- // This makes the assumption (in a debug build) that the block's heap
- // has the same memory hooks installed as this one. In reality this is the
- // case; but at some future point it might cause trouble......
- // In any case this will not occur in a non-debug build.
-
- this->ValidateMagicNumber();
- #if MM_DEBUG
- ptr = this->CallAboutToBlockSizeHooks(ptr);
- #endif
- return this->DoGetBlockHeap(ptr);
- }
-
-
-
- #endif
-